package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.CustomerDao;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 里
 * @Description:
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    //注入dao层
    @Autowired
    private CustomerDao customerDao;


    @Override
    public Map<String, Object> getCustomerListPage(Integer page, Integer rows, String customerName) {
        //定义一个map集合 用作返回key-》value数据
        Map<String,Object> customerMap = new HashMap<>();
        List<Customer> customerList =  customerDao.getSupplierPageList(page,rows,customerName);
        //判断集合是否为空，不为空则遍历到map集合中
        if (!CollectionUtils.isEmpty(customerList)) {
            customerMap.put("total",customerDao.getCustomerCount());
            customerMap.put("rows",customerList);
        }
        return customerMap;
    }

    @Override
    public void saveOrUpdateById(Customer customer) {
        if (customer.getCustomerId() != null) {
            customer =  customerDao.getCustomerById(customer.getCustomerId());
            customerDao.update(customer);
        }else {
            customerDao.saveCustomer(customer);
        }
    }

    @Override
    public void deleteCustomer(List<String> ids) {
        customerDao.deleteCustomer(ids);
    }
}
